package com.github.gin.springboot.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.gin.springboot.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by GinPonson on 12/14/2016.
 */
@Configuration
@ConditionalOnClass(HelloService.class)
@EnableConfigurationProperties(AuthorProperties.class)
@ConditionalOnProperty(prefix = "author",value = "enabled",matchIfMissing = true)
public class AuthorAutoConfiguration {

    private final AuthorProperties authorProperties;

    public AuthorAutoConfiguration(AuthorProperties authorProperties) {
        this.authorProperties = authorProperties;
    }

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() throws JsonProcessingException {
        HelloService helloService = new HelloService();
        helloService.setMsg(authorProperties.getName() +" :" + authorProperties.getPwd());

        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("arrayProps: " + objectMapper.writeValueAsString(authorProperties.getArrayProps()));
        System.out.println("listProp1: " + objectMapper.writeValueAsString(authorProperties.getListProp1()));
        System.out.println("listProp2: " + objectMapper.writeValueAsString(authorProperties.getListProp2()));
        System.out.println("mapProps: " + objectMapper.writeValueAsString(authorProperties.getMapProps()));
        System.out.println("Props: " + objectMapper.writeValueAsString(authorProperties.getProperties()));
        return helloService;
    }
}
